package controller.Common;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartFile;
import service.FleasMarketManagement.FleasMarketService;
import service.UserManagement.UserService;
import util.ResponseMessage;

@Controller
@RequestMapping("/FileUpload")
public class FileUploadController implements ServletContextAware {

    @Autowired
    private UserService userService;

    @Autowired
    private FleasMarketService fleasMarketService;

    private ServletContext servletContext;
    @Override
    public void setServletContext(ServletContext servletContext) {

        this.servletContext = servletContext;
    }

    @RequestMapping(value="user_upload")
    public String imageEdit() {
        return "user/user_upload";
    }


    @CrossOrigin(allowCredentials="true")
    @ResponseBody
    @PostMapping("/upload.do")
    public ResponseMessage upload(@RequestParam("image") MultipartFile image, HttpServletRequest request, ModelMap map) throws IOException {

        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        //获取存放文件在服务器中的路径,Tomcat/webapps/root/upload/*
//        String path = servletContext.getRealPath("/upload");
//        String path = servletContext.getRealPath("upload");
//
//        //判断文件夹是否存在，不存在则创建
//        File file = new File(path);
//        if(!file.exists()) {
//            file.mkdirs();
//        }

        //获取上传文件的文件名字
        String imgName = image.getOriginalFilename();
        //获取文件名字的后缀
        String contextType = imgName.substring(imgName.lastIndexOf("."));
        //创建新的文件名字
        String newName = new Date().getTime()+contextType;

        //声明输入流， 进行图片传输
        InputStream is = image.getInputStream();
        //指定输出图片的位置
        //OutputStream os = new FileOutputStream(new File("/usr/local/Tomcat/apache-tomcat-9.0.33/webapps/oneschool/upload/"+newName));
        //OutputStream os = new FileOutputStream(new File("/usr/local/Tomcat/apache-tomcat-9.0.33/webapps/oneschool/Static-Resources/images/"+newName));
        OutputStream os = new FileOutputStream(new File("/usr/local/Tomcat/apache-tomcat-9.0.33/webapps/ROOT/upload/"+newName));

        //定义byte数组
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len=is.read(buffer))!=-1) {
            os.write(buffer, 0, len);
            os.flush();
        }
        os.close();
        is.close();
        //把上传的文件路径返回到页面
        map.put("imgName", imgName);
        map.put("path", "http://47.96.104.134:8080/upload/"+newName);
        userService.updateUserIcon("http://47.96.104.134:8080/upload/"+newName,Current_logged_in_user.getUserId());
//        userService.updateUserIcon("http://47.96.104.134:8080/upload/"+newName,"13163357255");
        return new ResponseMessage(ResponseMessage.ERROR,"上传成功",map);
    }

//    @CrossOrigin(allowCredentials="true")
//    @ResponseBody
//    @PostMapping("/uploadFleas.do")
//    public ResponseMessage uploadFleas(@RequestParam("shopId") String shopId,@RequestParam("image") MultipartFile image, HttpServletRequest request, ModelMap map) throws IOException {
//        HttpSession session = request.getSession();
//        User Current_logged_in_user = (User) session.getAttribute("user");
//        if (Current_logged_in_user == null) {
//            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
//        }
//
//        //获取上传文件的文件名字
//        String imgName = image.getOriginalFilename();
//        //获取文件名字的后缀
//        String contextType = imgName.substring(imgName.lastIndexOf("."));
//        //创建新的文件名字
//        String newName = new Date().getTime()+contextType;
//
//        System.out.println("shopId = "+shopId+" imgName= "+imgName);
//
//
//        //获取存放文件在服务器中的路径,Tomcat/webapps/root/upload/*
//        String path = servletContext.getRealPath("/upload");
////        String path = servletContext.getRealPath("upload");
////
////        //判断文件夹是否存在，不存在则创建
//        File file = new File(path);
//        if(!file.exists()) {
//            file.mkdirs();
//        }
//
//        //声明输入流， 进行图片传输
//        InputStream is = image.getInputStream();
//        //指定输出图片的位置
//        OutputStream os = new FileOutputStream(new File(path+"\\"+newName));
////        OutputStream os = new FileOutputStream(new File("/usr/local/Tomcat/apache-tomcat-9.0.33/webapps/ROOT/upload/"+newName));
//
//        //定义byte数组
//        byte[] buffer = new byte[1024];
//        int len = 0;
//        while((len=is.read(buffer))!=-1) {
//            os.write(buffer, 0, len);
//            os.flush();
//        }
//        os.close();
//        is.close();
//        //把上传的文件路径返回到页面
//        map.put("imgName", imgName);
//        map.put("path", "http://47.96.104.134:8080/upload/"+newName);
////        userService.updateUserIcon("http://47.96.104.134:8080/upload/"+newName,Current_logged_in_user.getUserId());
////        userService.updateUserIcon("http://47.96.104.134:8080/upload/"+newName,"13163357255");
//        fleasMarketService.updateFleasIcon("http://47.96.104.134:8080/upload/"+newName,shopId);
//        return new ResponseMessage(ResponseMessage.ERROR,"上传成功",map);
//    }

    @CrossOrigin(allowCredentials="true")
    @ResponseBody
    @PostMapping("/uploadFleas.do")
    public ResponseMessage uploadFleas(@PathParam("shopId")  String shopId, HttpServletRequest request) throws IOException {
//        @PathParam("image") MultipartFile image,
        System.out.println("shopId="+shopId);
        return new ResponseMessage(ResponseMessage.ERROR,"上传成功",shopId);
    }
}
